package com.braveman.im.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

import com.braveman.im.R;
import com.braveman.im.bean.MyUnitsBean;
import com.braveman.im.constant.Constants;
import com.braveman.im.manager.ApiManager;
import com.braveman.im.ui.activity.message.ChatAllActivity;
import com.braveman.im.ui.adapter.MyUnitsAdapter;
import com.braveman.im.utils.UtilsStyle;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.utils.ActivityUtils;
import com.ht.baselibs.utils.SharedPreferencesUtils;
import com.vondear.rxtools.RxBarTool;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by 昊昊 on 2017/10/12.
 */

/**
 * 我的单位
 */
public class MyUnitsActivity extends BaseActivity {

    private RecyclerView unitsListView;
    private List<MyUnitsBean> list = new ArrayList<>();
    private MyUnitsBean myUnitsBean;
    private MyUnitsAdapter myUnitsAdapter;


    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText("我的单位").setLeftBtnIcon(R.mipmap.back_black).setOnLeftBtnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtils.finishActivity(MyUnitsActivity.this);
            }
        });
    }

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected int initContentView() {
        UtilsStyle.setStatusBarMode(this, true);
        RxBarTool.FlymeSetStatusBarLightMode(getWindow(), true);
        RxBarTool.MIUISetStatusBarLightMode(getWindow(), true);
        return R.layout.activity_my_units;
    }

    @Override
    protected void initChildView() {
        unitsListView = findViewById(R.id.activity_my_units);
        unitsListView.setLayoutManager(new LinearLayoutManager(this));
        myUnitsAdapter = new MyUnitsAdapter(R.layout.item_my_units, list);
        unitsListView.setAdapter(myUnitsAdapter);
    }

    @Override
    protected void initChildData() {
        ApiManager.getApi().getUnits().enqueue(new NetCallBack<List<MyUnitsBean>>() {
            @Override
            protected void onSuccess(Call<List<MyUnitsBean>> call, Response<List<MyUnitsBean>> response) {
                if (response != null) {
                    List<MyUnitsBean> mb = response.body();
                    list.addAll(mb);
                    if (myUnitsAdapter != null) {
                        myUnitsAdapter.notifyDataSetChanged();
                    }
                }
            }
        });
    }

    @Override
    protected void setChildViewListener() {
        myUnitsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("user_id", list.get(position).imGroupId);
                SharedPreferencesUtils.put(Constants.COMPANY, list.get(position).imGroupId);
                bundle.putInt("states", 2);
                ActivityUtils.startActivityAndFinish(MyUnitsActivity.this, ChatAllActivity.class, bundle);

            }
        });
    }
}
